
import { success, error } from '@/utils/toast'
import { userCollectService } from '@/api/login'

export async function collectItem(item) {
  item.collect = !item.collect
  const o = {
    ctype:'F', 
    fileid:item.fileid
  }
  var resp = null
  if (item.collect) {
    o.op = "add"
    resp = await userCollectService(o)
  } else {
    o.op = "delete"
    resp = await userCollectService(o)
  }

  if (!resp || 0 !== resp.result) {
    error(resp.message)
    return
  }

  success("操作成功")
}

export function createElement() {
  var el = document.createElement("div");
  el.style.backgroundImage = "url(../static/micon.png)"
  el.style.backgroundRepeat = "no-repeat"
  el.style.backgroundSize = "24px 24px"
  el.style.width = "24px"
  el.style.height = "24px"
  el.style.position = "fixed"
  el.style.border = "none"
  el.style.overflowScrolling = "touch"
  el.style.zIndex  = 1001
  el.style.pointerEvents = "none"
  // el.style.backgroundAttachment = "fixed"
  el.style.overflow = "auto"
  el.style.overflowY = "auto"
  //overflow-y: scroll;
  el.style.transform = "transform, opacity 0.5s, 0.5s ease, ease"
  document.body.appendChild(el);
  return el
}

export function addAnimation(_this, e, exfunc) {
  var el = createElement()
  const H = -30   // 最高上抛高度
  const U = 2     // 网上抛时间
  const G = 9.8   // 重力加速度
  var originX = e.pageX
  var originY = e.clientY - 15

  const maxTime = Math.sqrt((2 * Math.abs(e.clientY - _this.$store.getters.getPosY)) / G) + U    //  总的时间
  const xRate = (originX - _this.$store.getters.getPosX) / maxTime

  var time = 0
  var hold = false
  var itv = setInterval(() => {
    time += 0.30

    const X = originX - xRate * time
    const Y = H + G * (time - U) * (time - U) / 2
    if (hold) {
      clearInterval(itv);
      document.body.removeChild(el);
      exfunc()
      // appendItem(_this, item)
      setTimeout(() => {
        _this.$store.commit('setAddHover', false);  
      }, 200)
      return
    } 
  
    const PosX = _this.$store.getters.getPosX
    const PosY = _this.$store.getters.getPosY
    
    if (((xRate > 0 && X <= PosX) && originY + Y >= PosY) || 
      ((xRate < 0 && X >= PosX) && originY + Y >= PosY)) {
       _this.$store.commit('setAddHover', true);  
      hold = true
    } else {
      el.style.left = X + "px"
      el.style.top  = Y + originY + "px"
    }
  }, 20)
}